const express = require('express');
const router = express.Router();
const GoodModel = require('../models/goods');
const multer = require('multer');
var storage = multer.diskStorage({
	destination: 'public/images',
	filename: function(req, res, cb) {
		cb(null, req.body.title + '.jpg')
	}
})
var upload = multer({
	storage: storage
})

router.get('/addshop',function(req,res){
	res.render('addshop',{
		title:'添加信息页面',
		user:req.session.user,		
	})
})
router.post('/add', upload.single('photo'), function(req, res) {
	const currentUser = req.session.user;
    req.body.author = currentUser.name;
	req.body.tag = req.body.tag;
	req.body.shopImg =  req.body.title + '.jpg';
	req.body.address = req.body.address;
	
	var goodFabu =  new GoodModel(req.body);
	goodFabu.time = goodFabu.timeFabu();
	
	 goodFabu.save(function(err){
        if(err){
            res.json({error:1,message:'出错'});
        }
        res.json({error:0,message:'添加成功'});
    })
})






module.exports = router;
